const fs = require('fs')
const window = {}
const key = 'template'
let configs = fs.readFileSync('./public/config/index.js', 'utf-8')

// eslint-disable-next-line no-eval
eval(configs)
if (window.servers && window.servers[key]) {
  // 修改vue.config.js
  let vueConfigs = fs.readFileSync('./vue.config.js', 'utf-8')
  const oldVueConfigs = vueConfigs
  vueConfigs = vueConfigs.replace(/((?:const|let|var)\s+templateName\s*=\s*)((?:'|").*(?:'|"))/, (_, a, b) => {
    return a + `'${window.servers[key]}'`
  })
  if (oldVueConfigs !== vueConfigs) {
    fs.writeFileSync('./vue.config.js', vueConfigs)
    console.log('模板已切换')
  }

  // 修改main.js
  let mainJs = fs.readFileSync('./src/main.js', 'utf-8')
  const oldMainJs = mainJs

  mainJs = mainJs.replace(/(import\s+(?:'|").*template\/)([^/]*)(\/main(?:'|"))/, (_, a, b, c) => {
    return a + window.servers[key] + c
  })
  if (oldMainJs !== mainJs) {
    fs.writeFileSync('./src/main.js', mainJs)
  }
}

module.exports = (api, options) => {

}
